package com.bimbimbip.explorer;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class DeleteFolder extends Activity implements Runnable{
	Thread thread ;
	int i = 0;
	boolean stop = false ;
	TextView info;
	Button stopDelete;
	String name;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.delete_foder);
		info = (TextView)findViewById(R.id.delete_info);
		
		name = getIntent().getExtras().getString("foldername");
		Log.v("Folder delete path 22222222222222222222", name);
		info.setText("Deleting " + name);
		stopDelete = (Button)findViewById(R.id.stop_delete);
		stopDelete.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				stop = true;
				finish();
			}
		});
		thread = new Thread(this);
		thread.start();
		try {
			thread.join();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
	}


	public void run() {
		// TODO Auto-generated method stub
		deleteFolder(name);
		Log.v("Folder delete path", name);
		finish();
	}
	
	public void deleteFolder(String name){
		if(stop)
			return ;
		File file = new File(name);
		try {
			for(String str:file.list()){
				File childPath = new  File(name +"/" +str);
				if(childPath!=null){
						if(childPath.isFile()){
							if(!childPath.delete()){
								Toast.makeText(DeleteFolder.this, "Can not Delete file   " + childPath.getAbsolutePath() , Toast.LENGTH_SHORT).show();
								
							}
							
							
						}else deleteFolder(childPath.getAbsolutePath());
				}
				
				if(stop)
					return;
			}
				
		} catch (Exception e) {
			// TODO: handle exception
			
		}
		try {
			file.delete();
			Toast.makeText(DeleteFolder.this, "delete success !!", Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		finish();
	}

}
